package com.cxb.apipassenger.service;

import com.cxb.apipassenger.remote.ServicePassengerUserClient;
import com.cxb.apipassenger.remote.ServiceVerificationcodeClient;
import com.cxb.internalcommon.constant.CommonStatusEnum;
import com.cxb.internalcommon.constant.IdentityConstant;
import com.cxb.internalcommon.constant.TokenConstants;
import com.cxb.internalcommon.dto.ResponseResult;
import com.cxb.internalcommon.request.VerificationCodeDTO;
import com.cxb.internalcommon.responese.NumberCodeResponse;
import com.cxb.internalcommon.responese.TokenResponse;
import com.cxb.internalcommon.util.JwtUtils;
import com.cxb.internalcommon.util.RedisPrefixUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.temporal.ValueRange;
import java.util.concurrent.TimeUnit;

@Service
public class VerificationCodeService {

    @Autowired
    private ServiceVerificationcodeClient serviceVerificationcodeClient;
    @Autowired
    private ServicePassengerUserClient servicePassengerUserClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 生成验证码
     * @param passengerPhone 手机号
     * @return
     */
    public ResponseResult  generatorCode(String passengerPhone){

        //调用验证码微服务service-verificationcode,获取验证码
        ResponseResult<NumberCodeResponse> numberCodeRes = serviceVerificationcodeClient.getNumberCode(6);
        int numberCode = numberCodeRes.getData().getNumberCode();


        //过期时间
        String key = RedisPrefixUtils.generatorKeyByPhone(passengerPhone,IdentityConstant.PASSENGER_IDENTITY);
        //存入redis
        stringRedisTemplate.opsForValue().set(key,numberCode+"",2, TimeUnit.MINUTES);

        //通过短信服务商，将对应的验证码发送到手机上
        return ResponseResult.success("");
    }


    /**
     * 校验验证码
     * @param passengerPhone 手机号
     * @param verificationCode 验证码
     * @return
     */
    public  ResponseResult checkCode(String passengerPhone,String verificationCode){

        //根据手机号去redis读取验证码
        String key = RedisPrefixUtils.generatorKeyByPhone(passengerPhone,IdentityConstant.PASSENGER_IDENTITY);
        String codeRedis = stringRedisTemplate.opsForValue().get(key);
        System.out.println(codeRedis+"....");

        //校验验证码
        //已经过期
        if(StringUtils.isEmpty(codeRedis)){
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_EXPIRE.getCode(),
                    CommonStatusEnum.VERIFICATION_CODE_EXPIRE.getValue());
        }
        //不正确
        if(!verificationCode.trim().equals(codeRedis.trim())){
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),
                    CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }
        //判断原来是否有用户，并进行相应的处理
        VerificationCodeDTO verificationCodeDTO = new VerificationCodeDTO();
        verificationCodeDTO.setPassengerPhone(passengerPhone);
        servicePassengerUserClient.loginOrRegister(verificationCodeDTO);

        //颁发令牌
        String accessToken = JwtUtils.generatorToken(passengerPhone, IdentityConstant.PASSENGER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        String refreshToken = JwtUtils.generatorToken(passengerPhone, IdentityConstant.PASSENGER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);

        //将token存在redis
        String  accessTokenKey = RedisPrefixUtils.generatorTokenKey(passengerPhone,IdentityConstant.PASSENGER_IDENTITY,TokenConstants.ACCESS_TOKEN_TYPE);


        String  refreshTokenKey = RedisPrefixUtils.generatorTokenKey(passengerPhone,IdentityConstant.PASSENGER_IDENTITY,TokenConstants.REFRESH_TOKEN_TYPE);
        stringRedisTemplate.opsForValue().set(accessTokenKey,accessToken,30,TimeUnit.DAYS);
        stringRedisTemplate.opsForValue().set(refreshTokenKey,refreshToken,31,TimeUnit.DAYS);


        //响应
        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);
        return ResponseResult.success(tokenResponse);
    }

}
